package com.shengyuan.manage_os.old.ying.erpm.db;

import java.util.List;

import com.shengyuan.manage_os.old.ying.erpm.data.Machinery;

import android.content.Context;

public class Actual_JiShi_Table extends DBHelper {

	private String Actual_Jishi_Table = "actual_jishi_table";
	private String sql;
	public final static String JIXIENAME="JIXIENAME",  STATE = "STATE", STARTTOEND="STARTTOEND", ID="ID";
	public Actual_JiShi_Table(Context ctx) {
		super(ctx);
		open();
		boolean condition = isTableExist(Actual_Jishi_Table);
		if (!condition) {
			sql = "CREATE TABLE "
					+ Actual_Jishi_Table
					+ "(JIXIENAME VARCHAR, STATE INTEGER, STARTTOEND VARCHAR, ID INTEGER)";
			execSQL(sql);
		}
	}
	

	/*
	 * 机械状态保存表
	 * jixiename 机械名称
	 * state =0 待命 1 运行  2 暂停
	 * 
	 */
	public void Save_FirstStartRecord(String jixiename, String starttime, int id) {
		String sql = "UPDATE " + Actual_Jishi_Table 
				+ " SET " + STATE + " = 1, " + STARTTOEND + "= '" +  starttime +"', ID=" + id 
				+ " WHERE "+ JIXIENAME +"='" + jixiename + "'";
		mDb.execSQL(sql ) ;
	}
	
	public void Save_PauseRecord(String jixiename, Integer state, String time) {
		String sql = "UPDATE " + Actual_Jishi_Table 
				+ " SET " + STATE + " = " + state +", " + STARTTOEND + "=" + STARTTOEND + "||'," +  time +"'" 
				+ " WHERE "+ JIXIENAME +"='" + jixiename + "'";
		mDb.execSQL(sql ) ;
	}
	
	public void Save_FinishedRecord(String jixiename) {
		String sql = "UPDATE " + Actual_Jishi_Table 
				+ " SET " + STATE + " = 0, " + STARTTOEND + "='' "  
				+ " WHERE "+ JIXIENAME +"='" + jixiename + "'";
		mDb.execSQL(sql ) ;
	}	
	
	public int getState(String name) {
		String sql = "SELECT "+ STATE +" FROM " + Actual_Jishi_Table 
				+ " WHERE " + JIXIENAME + "='"+ name +"'";
		int state = GetValue(sql);
		return state;
	}

	public int getId(String name) {
		String sql = "SELECT "+ ID +" FROM " + Actual_Jishi_Table 
				+ " WHERE " + JIXIENAME + "='"+ name +"'";
		int state = GetValue(sql);
		return state;
	}
	
	public String getTime(String name) {
		String sql = "SELECT " + STARTTOEND + " FROM " + Actual_Jishi_Table 
				+ " WHERE " + JIXIENAME + "='"+ name +"'";
		String result = GetString(sql);
		return result;
	}
	
	public void InitRows(List<Machinery> MachineryItems) {
		String name="";
		for (int i = 0; i < MachineryItems.size(); i++) {
			Machinery machinery = MachineryItems.get(i);
			name = machinery.Name.trim();
			String sql="SELECT COUNT(*) FROM " + Actual_Jishi_Table + " WHERE " + JIXIENAME + "='"+ name +"'";
			int count = GetValue(sql);
			if (count == 0) {
				Insert_Row( name );
			}
		}
	}
	
	private void Insert_Row(String name) {
		String sql ="INSERT INTO " + Actual_Jishi_Table + 
					" (JIXIENAME, STATE, STARTTOEND)" + 
					" VALUES('"+ name +"',0,'')";
		mDb.execSQL(sql);
	}

	/*
	 * finished=0 完成一半工作 
	 * finished=1 全部完成
	 */
//	private Cursor List_Finished(int finished, String form_type) {
//		return findList(Table_actual_form, new String[]{JSON1}, 
//				"sended=0 and finished=? and user=? and form_type=?", 
//				new String[]{finished+"", LoginActivity.pmUser.v_yhdm, form_type},null, null, null  );
//	}
//	
//	
//	public ArrayList<String> getArray_Finished(int finished, String form_type) {
//		ArrayList<String> list = new ArrayList<String>();
//		Cursor cursor = List_Finished(finished, form_type);
//		while (cursor.moveToNext()) {
//			String s = cursor.getString(0);
//			list.add(s);
//		}
//		return list;
//	}
	
}
